package com.daykw.wkinfo.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * Created by guoyu on 2017/5/11.
 */
@Component
public class SpringContextHolder implements ApplicationContextAware {
	private static ApplicationContext applicationContext;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		SpringContextHolder.applicationContext = applicationContext;
	}

	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	public static <T> T getBean(Class<T> name) throws BeansException {
		return (T) applicationContext.getBean(name);
	}
	public static <T> T getBean(Class<T> name,Object... args) throws BeansException {
		return (T) applicationContext.getBean(name,args);
	}

	// 通过名字获取上下文中的bean
	public static <T> T getBean(String name) {
		return (T) applicationContext.getBean(name);
	}
}
